package com.elitetyc.eliteim.client.handler.impl;

import cn.hutool.json.JSONUtil;
import com.elitetyc.eliteim.client.config.AllInfo;
import com.elitetyc.eliteim.client.handler.MsgSuccessHandler;
import com.elitetyc.eliteim.common.constant.Constants;
import com.elitetyc.eliteim.common.model.impl.IMUserInfoImpl;
import com.elitetyc.eliteim.common.model.impl.UserLoginData;
import com.elitetyc.eliteim.common.proto.RequestProto;
import com.elitetyc.eliteim.common.util.UserInfoHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yuanchao.tan
 * @version 1.0
 * @description 登陆回调处理
 *
 * 登陆成功将当前用户信息设置到客户端信息中去
 * @Date 2022/6/17 16:57
 */
@Component
@Slf4j
public class LoginMsgSuccessHandlerImpl implements MsgSuccessHandler {

    @Autowired
    private AllInfo allInfo;

    @Override
    public boolean isSupport(RequestProto.RequestProtocol msg) {
        return msg.getType()== Constants.RequestType.LOGIN;
    }

    @Override
    public void handle(RequestProto.RequestProtocol msg) {
        final UserLoginData loginData = JSONUtil.toBean(msg.getData(), UserLoginData.class);
        allInfo.setClient(new AllInfo.ClientInfo(loginData.getUserId(),loginData.getUserName()));
        // 设置用户信息，正式环境业务应该是通过接口获取用户信息
        UserInfoHolder.user = new IMUserInfoImpl(loginData.getUserId(),loginData.getUserName());
        log.info("登陆成功~");
    }
}
